<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/1/19
 * Time: 11:54
 */
namespace app\common\helper;

use app\modules\setting\models\jlpjk\SetJlpjkFlgl;
use app\modules\setting\models\jlpjk\SetJlpjkSblb;
use app\modules\setting\models\jlpjk\SetJlpjkWzgl;
use app\modules\setting\models\zwgl\SetZwglGwlb;
use yii\helpers\ArrayHelper;
use yii\helpers\Html as H;
use yii\helpers\Json;

class CommonHelper
{
    static function rowDiv($content,$option = ['class'=>'col-md-6']){
        $str = "";
        if(is_array($content)){
            foreach ($content as $item){
                $str .= H::tag("div",$item,$option);
            }
        }else{
            $str = H::tag("div",$content,$option);
        }
        return H::tag("div",$str ,['class' =>"row"]);
    }

    static function age($birthday){
        try{
            $datetimeStart = new \DateTime($birthday);
            $datetimeEnd = new \DateTime();
            // 计算两个日期之间的差值
            $interval = $datetimeStart->diff($datetimeEnd);

            // 输出相差的年数
            $str =  $interval->format("%y岁");
        }catch (\Throwable $e){
            $str = "无法解析";
        }

        return $str;
    }

    static function getTreeList($index_list , &$list , $tree_item , $field="id", $field_children = 'children'){
        if(ArrayHelper::getValue($tree_item,"0.{$field}")){
            foreach ($tree_item as $item){
                $id = ArrayHelper::getValue($item,$field);
                $item_list = ArrayHelper::getValue($index_list,$id);
                if($item_list){
                    $list[] = $item_list;
                }
                if(isset($item[$field_children])){
                    self::getTreeList($index_list , $list , $item[$field_children] , $field, $field_children );
                }
            }
        }else{
            $item = $tree_item;
            $id = ArrayHelper::getValue($item,$field);
            $item_list = ArrayHelper::getValue($index_list,$id);
            if($item_list){
                $list[] = $item_list;
            }
            if(isset($item[$field_children])){
                self::getTreeList($index_list , $list , $item[$field_children] , $field, $field_children );
            }
        }
    }

    static $gw_list = [];
    static function getGwList(){
        if(empty(static::$gw_list)){
            $rows = SetZwglGwlb::find()->asArray()->all();
            static::$gw_list = ArrayHelper::index($rows,'id');
        }
        return static::$gw_list;
    }
    static function getGwParent($pid){
        $gw_list = static::getGwList();
        return ArrayHelper::getValue($gw_list,$pid);
    }

    static function getTop($item){
        $gw_list = static::getGwList();
        if(($pid = ArrayHelper::getValue($item,'pid')) && ($item_pid = ArrayHelper::getValue($gw_list,$pid))){
            return static::getTop($item_pid);
        }
        return $item;
    }

    static function getGwName($item,$name = ''){
        if(empty($item)){
            return $name;
        }
        if(empty($name)){
            $name = $item['mc'];
        }else{
            $name = "【{$item['mc']}】-{$name}";
        }
        if(isset($item['pid']) && $item['pid']){
            $parent = static::getGwParent($item['pid']);
            $name = static::getGwName($parent,$name);
        }
        return $name;
    }


    static function treeFindName($tree , $id , $id_field = 'id', $is_list = true,$is_leaf = false){
        $path_name = '';
        if($is_list){
            foreach ($tree as $tree_item){
                if($is_leaf){
                    if(ArrayHelper::getValue($tree_item,'is_leaf') && ArrayHelper::getValue($tree_item,$id_field) == $id){
                        $path_name = ArrayHelper::getValue($tree_item,'path_name',$tree_item['title']);
                        break;
                    }
                }else{
                    if(ArrayHelper::getValue($tree_item,$id_field) == $id){
                        $path_name = ArrayHelper::getValue($tree_item,'path_name',$tree_item['title']);
                        break;
                    }
                }

                if(isset($tree_item['children']) && $tree_item['children']){
                    $name = static::treeFindName($tree_item['children'],$id,$id_field,true,$is_leaf);
                    if($name){
                        $path_name = $name;
                        break;
                    }
                }
            }
        }else{
            $tree_item = $tree;
            if($is_leaf){
                if(ArrayHelper::getValue($tree_item,'is_leaf') && ArrayHelper::getValue($tree_item,$id_field) == $id){
                    $path_name = ArrayHelper::getValue($tree_item,'path_name',$tree_item['title']);
                }
            }else{
                if(ArrayHelper::getValue($tree_item,$id_field) == $id){
                    $path_name = ArrayHelper::getValue($tree_item,'path_name',$tree_item['title']);
                }
            }

            if(isset($tree_item['children']) && $tree_item['children']){
                $name = static::treeFindName($tree_item['children'],$id,$id_field,true,$is_leaf);
                if($name){
                    $path_name = $name;
                }
            }
        }
        return $path_name;
    }

    /**
     * 构建设备数据的树形结构
     */
    public static function sbTree(){
        return TreeMapHelper::getInstance(TreeMapHelper::TREE_SHE_BEI)->getOptions();
    }

    /**
     * 构建位置的树形结构
     */
    public static function wzTree(){
        return TreeMapHelper::getInstance(TreeMapHelper::TREE_WEI_ZHI_EXT_FL_CONFIG)->getOptions();
    }


    public static function getExtData(&$params){
        $ext = [];
        if(is_array($params) && $params){
            foreach ($params as $k => $v){
                if(preg_match('/f\d+/',$k)){
                    $ext[$k] = $v;
                    unset($params[$k]);//注销对应数据
                }
            }
        }
      return Json::encode($ext);
    }
}